package com.unionftech.common.common.presenter

import com.unionftech.common.base.mvp.IBaseView
import com.unionftech.common.base.mvp.impl.BasePresenter
import com.unionftech.common.base.mvp.impl.IBaseCallBack
import com.unionftech.common.bean.ResponseResult
import com.unionftech.common.common.bean.AppVersionInfo
import com.unionftech.common.common.bean.BannerImageInfo
import com.unionftech.common.common.bean.BannerPosition
import com.unionftech.common.common.bean.CountryInfo
import com.unionftech.common.common.bean.UserKYCFileInfo
import com.unionftech.common.common.bean.PhoneRegexInfo
import com.unionftech.common.common.bean.TradePlatform
import com.unionftech.common.common.bean.SystemUserInfo
import com.unionftech.common.common.bean.UserGroupInfo
import com.unionftech.common.common.bean.request.SendCodeRequest
import com.unionftech.common.common.bean.request.SendEmailCodeRequest
import com.unionftech.common.common.contract.CommonContract
import com.unionftech.common.common.model.CommonModel

class CommonPresenter : BasePresenter<IBaseView, CommonModel>(), CommonContract.ICommonPresenter {
    override fun requestCountryList() {
        getView()?.showLoading()
        getModel().execCountryList(object : IBaseCallBack<ResponseResult<List<CountryInfo>>> {
            override fun onResponse(response: ResponseResult<List<CountryInfo>>) {
                getView()?.also { view ->
                    view.hideLoading()
                    if (view is CommonContract.ICountryView) {
                        view.handleCountryList(response)
                    }
                }
            }
        })
    }

    override fun getAllCountryPhoneRegex() {
        getView()?.showLoading()
        getModel().getAllCountryPhoneRegex(object :
            IBaseCallBack<ResponseResult<List<PhoneRegexInfo>>> {
            override fun onResponse(response: ResponseResult<List<PhoneRegexInfo>>) {
                getView()?.also { view ->
                    view.hideLoading()
                    if (view is CommonContract.IPhoneRegexView) {
                        view.handleAllPhoneRegex(response)
                    }
                }
            }
        })
    }

    override fun queryCountryByIp() {
        getView()?.showLoading()
        getModel().queryCountryByIp(object : IBaseCallBack<ResponseResult<CountryInfo>> {
            override fun onResponse(response: ResponseResult<CountryInfo>) {
                getView()?.also { view ->
                    view.hideLoading()
                    if (view is CommonContract.IDefaultView) {
                        view.handleDefaultCountryInfo(response)
                    } else if (view is CommonContract.IDefaultCountryView) {
                        view.handleDefaultCountryInfo(response)
                    }
                }
            }
        })
    }

    override fun getPublicKey() {
        getView()?.showLoading()
        getModel().getPublicKey(object : IBaseCallBack<ResponseResult<String>> {
            override fun onResponse(response: ResponseResult<String>) {
                getView()?.also { view ->
                    view.hideLoading()
                    if (view is CommonContract.IDefaultView) {
                        view.handlePublicKey(response)
                    } else if (view is CommonContract.IPublicKeyView) {
                        view.handlePublicKey(response)
                    }
                }
            }
        })
    }


    override fun execSendCode(requestBean: SendCodeRequest, token: String?) {
        getView()?.showLoading()
        val callback = object : IBaseCallBack<ResponseResult<Any?>> {
            override fun onResponse(response: ResponseResult<Any?>) {
                getView()?.hideLoading()
                getView()?.let {
                    if (it is CommonContract.ISendCodeView) {
                        it.handleSendCode(response)
                    }
                }
            }
        }
        getModel().execSendCode(requestBean, callback, token)
    }

    override fun sendCodeRegister(requestBean: SendCodeRequest) {
        getView()?.showLoading()
        val callback = object : IBaseCallBack<ResponseResult<Any?>> {
            override fun onResponse(response: ResponseResult<Any?>) {
                getView()?.hideLoading()
                getView()?.let {
                    if (it is CommonContract.ISendCodeView) {
                        it.handleSendCode(response)
                    }
                }
            }
        }
        getModel().sendCodeRegister(requestBean, callback)
    }

    override fun getOuterUserInfo(token: String) {
        getView()?.showLoading()
        getModel().getOuterUserInfo(
            token,
            object :
                IBaseCallBack<ResponseResult<SystemUserInfo>> {
                override fun onResponse(response: ResponseResult<SystemUserInfo>) {
                    getView()?.hideLoading()
                    getView()?.let {
                        if (it is CommonContract.IAccountView) {
                            it.handleOuterUserInfoResult(response)
                        }
                    }
                }
            })
    }

    override fun getKycInfo(token: String) {
        getView()?.showLoading()
        getModel().getKycInfo(token,
            object :
                IBaseCallBack<ResponseResult<UserKYCFileInfo>> {
                override fun onResponse(response: ResponseResult<UserKYCFileInfo>) {
                    getView()?.hideLoading()
                    getView()?.let {
                        if (it is CommonContract.IKycInfoView) {
                            it.handleKycInfoResult(response)
                        }
                    }
                }
            })
    }

    override fun checkInvitationCode(token: String, code: String) {
        getView()?.showLoading()
        val callback = object : IBaseCallBack<ResponseResult<Any?>> {
            override fun onResponse(response: ResponseResult<Any?>) {
                getView()?.hideLoading()
                getView()?.let {
                    if (it is CommonContract.ICheckCodeView) {
                        it.handleCheckInvitationCode(response)
                    }
                }
            }
        }
        getModel().checkInvitationCode(token, code, callback)
    }


    override fun execSendEmailCode(requestBean: SendEmailCodeRequest) {
        getView()?.showLoading()
        val callBack = object : IBaseCallBack<ResponseResult<Any?>> {
            override fun onResponse(response: ResponseResult<Any?>) {
                getView()?.hideLoading()
                getView()?.let {
                    if (it is CommonContract.ISendCodeView) {
                        it.handleSendCode(response)
                    }
                }
            }
        }
        getModel().execSendEmailCode(requestBean, callBack)
    }

    override fun querySystemDemoPlatform(token: String) {
        getView()?.showLoading()
        val callBack = object : IBaseCallBack<ResponseResult<List<TradePlatform>>> {
            override fun onResponse(response: ResponseResult<List<TradePlatform>>) {
                getView()?.hideLoading()
                getView()?.let {
                    if (it is CommonContract.IPlatformView) {
                        it.handlePlatformView(response)
                    }
                }
            }
        }
        getModel().querySystemDemoPlatform(token, callBack)
    }

    override fun queryPlatform(token: String, code: String) {
        getView()?.showLoading()
        val callBack = object : IBaseCallBack<ResponseResult<List<TradePlatform>>> {
            override fun onResponse(response: ResponseResult<List<TradePlatform>>) {
                getView()?.hideLoading()
                getView()?.let {
                    if (it is CommonContract.IPlatformView) {
                        it.handlePlatformView(response)
                    }
                }
            }
        }
        getModel().queryPlatform(token, code, callBack)
    }

    override fun queryGroupAndCurrency(token: String, code: String) {
        getView()?.showLoading()
        val callBack = object : IBaseCallBack<ResponseResult<UserGroupInfo>> {
            override fun onResponse(response: ResponseResult<UserGroupInfo>) {
                getView()?.hideLoading()
                getView()?.let {
                    if (it is CommonContract.ICurrencyView) {
                        it.handleGroupCurrencyView(response)
                    }
                }
            }
        }
        getModel().queryGroupAndCurrency(token, code, callBack)
    }

    override fun queryAccountType(token: String, code: String) {
        getView()?.showLoading()
        val callBack = object : IBaseCallBack<ResponseResult<List<Int>>> {
            override fun onResponse(response: ResponseResult<List<Int>>) {
                getView()?.hideLoading()
                getView()?.let {
                    if (it is CommonContract.IAccountTypeView) {
                        it.handleAccountTypeView(response)
                    }
                }
            }
        }
        getModel().queryAccountType(token, code, callBack)
    }

    override fun queryCurrency(token: String, code: String, platform: String, tradeType: String) {
        getView()?.showLoading()
        val callBack = object : IBaseCallBack<ResponseResult<List<String>>> {
            override fun onResponse(response: ResponseResult<List<String>>) {
                getView()?.hideLoading()
                getView()?.let {
                    if (it is CommonContract.ICurrencyView) {
                        it.handleCurrencyView(response)
                    }
                }
            }
        }
        getModel().queryCurrency(token, code, platform, tradeType, callBack)
    }

    override fun getBannerList(position: BannerPosition) {
        getView()?.showLoading()
        val callBack = object : IBaseCallBack<ResponseResult<List<BannerImageInfo>>> {
            override fun onResponse(response: ResponseResult<List<BannerImageInfo>>) {
                getView()?.hideLoading()
                getView()?.let {
                    if (it is CommonContract.IBannerView) {
                        it.handleBannerView(position, response)
                    }
                }
            }
        }
        getModel().getBannerList(position, callBack)
    }

    override fun getUpdateInfo() {
        val callBack = object : IBaseCallBack<ResponseResult<AppVersionInfo>> {
            override fun onResponse(response: ResponseResult<AppVersionInfo>) {
                getView()?.let {
                    if (it is CommonContract.IUpdateView) {
                        it.handleUpdateView(response)
                    }
                }
            }
        }
        getModel().getUpdateInfo(callBack)
    }
}